﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Tauren.Form.Entity.Output;
using Tauren.Form.Infrastructure;
using Tauren.Form.Infrastructure.Common;
using Tauren.Form.Service.Field_FormBLL;

namespace Tauren.Form.Core.Controllers
{
    /// <summary>
    /// 表单信息接口-v2.0
    /// </summary>
    [Route("api/form/v{version:apiVersion}")]
    [ApiController]
    public class Field_FormController : ControllerBase
    {
        private IField_FormService service;
        private IFieldService fieldService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        /// <param name="_fieldService"></param>
        public Field_FormController(IEnumerable<IField_FormService> services, IFieldService _fieldService)
        {
            this.service = services.FirstOrDefault(o => o.VersionName == 1.0);
            if (this.service == null) throw new ApiVersionException("不支持当前版本");
            this.fieldService = _fieldService;
        }
        /// <summary>
        /// 分页获取 单位信息列表
        /// </summary>
        /// <param name="queryModel"></param>
        /// <returns></returns>
        [HttpPost, Route("pages")]
        [ApiVersion("1.0")]
        public IActionResult Pages([FromBody] Entity.Global.QueryModel queryModel) => service.Pages(queryModel).ToJsonResult();

        /// <summary>
        /// 获取单个表信息数据
        /// </summary>
        /// <param name="id">人员基本信息实体id</param>
        /// <returns></returns>
        [HttpPost, Route("get/{id}")]
        public IActionResult Get([FromRoute] long id) => service.FirstOrDefault(id).ToJsonResult();
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Route("add")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Add([FromBody] Entity.Input.Field_Form_InPut input) => service.Add(input).ToJsonResult();


        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Route("edit")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Editor([FromBody] Entity.Input.Field_Form_InPut input) => service.Edit(input).ToJsonResult();
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">实体id</param>
        /// <returns></returns>
        [HttpPost, Route("del/{id}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Delete([FromRoute] int id) => service.Delete(id).ToJsonResult();
        /// <summary>
        /// 启用??
        /// </summary>
        /// <param name="id">ID</param> 
        /// <returns></returns>
        [HttpPost, Route("uninstall/{id}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Uninstall(int id) => service.Uninstall(id).ToJsonResult();

        /// <summary>
        /// 表单字段
        /// </summary>
        /// <param name="FormId">ID</param> 
        ///  /// <param name="VersionId">表单版本ID</param> 
        /// <returns></returns>
        [HttpPost, Route("fields/{FormId}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult FormFields([FromRoute] long FormId, [FromQuery] long VersionId = 0) => fieldService.FormFields(FormId, VersionId).ToJsonResult();

        /// <summary>
        /// 添加区域信息
        /// </summary>
        /// <param name="queryModel"></param>
        /// <returns></returns>
        [HttpPost, Route("tables")]
        [ApiVersion("1.0")] 
        public IActionResult Tables([FromBody] Entity.Global.QueryModel queryModel) => fieldService.TableInfos(queryModel).ToJsonResult();

        /// <summary>
        /// 保存字段信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        [HttpPost, Route("save")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Save([FromBody] Field_Form_Version_InPut Model) => fieldService.Save(Model).ToJsonResult();

        /// <summary>
        /// 发布字段信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        [HttpPost, Route("publish")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Publish([FromBody] Field_Form_Version_InPut Model) => fieldService.Publish(Model).ToJsonResult();
    }
}
